﻿using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;
using YJTShiftManagement.Events;
using YJTShiftManagement.Service;
using YJTShiftManagement.Utils;

namespace YJTShiftManagement
{
    public partial class MainForm : UIForm
    {
        private DoctorShiftViews _doctorShiftPage;
        
        #region "窗口移动代码"
        private bool mIsMouseDown = false;
        private Point mFormLocation;     // Form的location
        private Point mMouseOffset;      // 鼠标的按下位置

        private void OnFormMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mIsMouseDown = true;
                mFormLocation = this.Location;
                mMouseOffset = Control.MousePosition;
            }
        }

        private void OnFormMouseUp(object sender, MouseEventArgs e)
        {
            mIsMouseDown = false;
        }

        private void OnFormMouseMove(object sender, MouseEventArgs e)
        {
            if (mIsMouseDown)
            {
                Point pt = Control.MousePosition;
                int x = mMouseOffset.X - pt.X;
                int y = mMouseOffset.Y - pt.Y;

                this.Location = new Point(mFormLocation.X - x, mFormLocation.Y - y);
            }
        }
        private void btnLogout_Click()
        {
            islogin = true;
            if (islogin) // 根据登录结果判断
            {
                Application.Restart(); // 重启应用而非关闭主窗体
            }
            else
            {
                this.Close();
            }
        }
        bool islogin = false;
        private void HandleStringEvent(string data)
        {
            if (data == "登录界面")
            {
                btnLogout_Click();
                islogin = true;
            }
            else
            {
                _doctorShiftPage.SetParameter(data); // 假设DoctorShiftViews有该方法
                uiNavBar1.SelectedIndex = 1;
            }

        }
        #endregion
        public MainForm()
        {
            InitializeComponent();

            this.MainTabControl = uiTabControl1;
            uiNavBar1.TabControl = uiTabControl1;
            TableLayoutPanel tableLayout = new TableLayoutPanel { Dock = DockStyle.Fill };
            
            this.FormClosing += MainForm_FormClosing; // 手动订阅事件
            // 订阅事件
            EventManager.Instance.OnStringEvent += HandleStringEvent;
            _doctorShiftPage = new DoctorShiftViews(""); // 初始参数为空
            AddPage(new ShiftManagementPage(), 1001);
            AddPage(_doctorShiftPage, 1002); // 使用保存的实例
            //设置Header节点索引
            uiNavBar1.SetNodePageIndex(uiNavBar1.Nodes[0], 1001);
            uiNavBar1.SetNodePageIndex(uiNavBar1.Nodes[1], 1002);
            //显示默认界面
            uiNavBar1.SelectedIndex = 0;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (islogin == true)
            {
                e.Cancel = false;
            }
            else
            {
                if (this.ShowAskDialog2($"消息内容：确定要退出程序吗？", true))
                {
                    var request = new { manager_id = GlobalApp.Manager_Id };
                    var result = BaseService.HttpService("api_back/api_login/logout", request);

                    if (result.status == "success" && null != result.data && false == string.IsNullOrEmpty(result.data.ToString()))
                    {

                    }
                    else
                    {
                        this.ShowErrorDialog(result.msg);
                        EventManager.Instance.RaiseStringEvent("登录界面");
                        return;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }
    }
}
